package com.hcbd.tdwt.ui.common.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.hcbd.tdwt.R;
import com.bumptech.glide.Glide;


import java.text.MessageFormat;

public class UpgradeDialog extends Dialog implements View.OnClickListener {

    private Button okBtn;
    private OnClickListener listener;
    private String version;
    private String upgradeContent;
    private TextView versionTv;
    private TextView upgradeContentTv;
    private ImageButton shutDown;
    private Context mContext;
    private ImageView topBg;
    public UpgradeDialog(Context context,String version,String upgradeContent,OnClickListener onClickListener) {
        super(context);
        this.mContext=context;
        this.listener = onClickListener;
        this.version=version;
        this.upgradeContent=upgradeContent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_upgrade);
        setCanceledOnTouchOutside(false);
        init();
    }

    public void init() {
        okBtn = findViewById(R.id.okBtn);
        versionTv=findViewById(R.id.versionTv);
        upgradeContentTv=findViewById(R.id.upgradeContentTv);
        shutDown=findViewById(R.id.shutDown);
        topBg=findViewById(R.id.topBg);
        okBtn.setOnClickListener(this);
        shutDown.setOnClickListener(this);
        versionTv.setText(MessageFormat.format("v{0}", version));
        upgradeContentTv.setText(upgradeContent);

        Glide.with(mContext).load(R.drawable.upgrade_dialog_bg).centerCrop().into(topBg);

    }


    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.okBtn:
                listener.onOk(this);
                this.dismiss();
                break;
            case R.id.shutDown:
                this.dismiss();
                break;
        }
    }

    public interface OnClickListener {
        void onOk(Dialog dialog);
    }


    public static void show(Dialog dialog) {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public static void shutDown(Dialog dialog) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}